home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / bison.lha / bison++-1.04 / files.c < prev    next >
C/C++ Source or Header  |  1989-07-08  |  7KB  |  303 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  25. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  26. #endif
  27.  
  28. #include <stdio.h>
  29. #include "files.h"
  30. #include "new.h"
  31. #include "gram.h"
  32.  
  33. FILE *finput = NULL;
  34. FILE *foutput = NULL;
  35. FILE *fdefines = NULL;
  36. FILE *ftable = NULL;
  37. FILE *fattrs = NULL;
  38. FILE *fguard = NULL;
  39. FILE *faction = NULL;
  40. FILE *fparser = NULL;
  41.  
  42. /* File name specified with -o for the output file, or 0 if no -o.  */
  43. char *spec_outfile;
  44.  
  45. char *infile;
  46. char *outfile;
  47. char *defsfile;
  48. char *tabfile;
  49. char *attrsfile;
  50. char *guardfile;
  51. char *actfile;
  52. char *tmpattrsfile;
  53. char *tmptabfile;
  54.  
  55. extern char    *mktemp();    /* So the compiler won't complain */
  56. extern char    *getenv();
  57. extern char *parser_name;
  58. FILE    *tryopen();    /* This might be a good idea */
  59. void done();
  60.  
  61. extern int verboseflag;
  62. extern int definesflag;
  63. int fixed_outfiles = 0;
  64.  
  65.  
  66. char*
  67. stringappend(string1, end1, string2)
  68. char *string1;
  69. int end1;
  70. char *string2;
  71. {
  72.   register char *ostring;
  73.   register char *cp, *cp1;
  74.   register int i;
  75.  
  76.   cp = string2;  i = 0;
  77.   while (*cp++) i++;
  78.  
  79.   ostring = NEW2(i+end1+1, char);
  80.  
  81.   cp = ostring;
  82.   cp1 = string1;
  83.   for (i = 0; i < end1; i++)
  84.     *cp++ = *cp1++;
  85.  
  86.   cp1 = string2;
  87.   while (*cp++ = *cp1++) ;
  88.  
  89.   return ostring;
  90. }
  91.  
  92.  
  93. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  94.    the output files, and opens the tmp files and the parser */
  95. void
  96. openfiles()
  97. {
  98.   char *name_base;
  99.   register char *cp;
  100.   char *filename;
  101.   int base_length;
  102.   int short_base_length;
  103.  
  104. #ifdef VMS
  105.   char *tmp_base = "sys$scratch:b_";
  106. #else
  107.   char *tmp_base = "/tmp/b.";
  108. #endif
  109.   int tmp_len = strlen (tmp_base);
  110.  
  111.   if (spec_outfile)
  112.     {
  113.       /* -o was specified.  The precise -o name will be used for ftable.
  114.      For other output files, remove the ".cc" or ".tab.cc" suffix.  */
  115.       name_base = spec_outfile;
  116.       /* BASE_LENGTH includes ".tab" but not ".cc".  */
  117.       base_length = strlen (name_base);
  118.       if (!strcmp (name_base + base_length - 3, ".cc"))
  119.     base_length -= 3;
  120.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".cc".  */
  121.       short_base_length = base_length;
  122.       if (!strcmp (name_base + short_base_length - 4, ".tab"))
  123.     short_base_length -= 4;
  124.       else if (!strcmp (name_base + short_base_length - 4, "_tab"))
  125.     short_base_length -= 4;
  126.     }
  127.   else
  128.     {
  129.       /* -o was not specified; compute output file name from input
  130.      or use y.tab.cc, etc., if -y was specified.  */
  131.  
  132.       name_base = fixed_outfiles ? "y.y" : infile;
  133.  
  134.       /* Discard any directory names from the input file name
  135.      to make the base of the output.  */
  136.       cp = name_base;
  137.       while (*cp)
  138.     {
  139.       if (*cp == '/')
  140.         name_base = cp+1;
  141.       cp++;
  142.     }
  143.  
  144.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  145.  
  146.       base_length = strlen (name_base);
  147.       if (!strcmp (name_base + base_length - 2, ".y"))
  148.     base_length -= 2;
  149.       short_base_length = base_length;
  150.  
  151. #ifdef VMS
  152.       name_base = stringappend(name_base, short_base_length, "_tab");
  153. #else
  154.       name_base = stringappend(name_base, short_base_length, ".tab");
  155. #endif
  156.       base_length = short_base_length + 4;
  157.     }
  158.  
  159.   finput = tryopen(infile, "r");
  160.  
  161.   filename = getenv ("BISON_SIMPLE");
  162.   fparser = tryopen(filename ? filename : PFILE, "r");
  163.  
  164.   parser_name = (char *) malloc ((unsigned) (short_base_length + 1) * sizeof(char));
  165.   {
  166.     char *s, *r;
  167.     for(s = name_base, r = parser_name; *s != '.';) *r++ = *s++ ;
  168.     *r = '\0';
  169.   }
  170.  
  171.   if (verboseflag)
  172.     {
  173.       outfile = stringappend(name_base, short_base_length, ".output");
  174.       foutput = tryopen(outfile, "w");
  175.     }
  176.  
  177.   defsfile = stringappend(name_base, base_length, ".h");
  178.   if (definesflag)
  179.     {
  180.       fdefines = tryopen(defsfile, "w");
  181.     }
  182.  
  183.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  184.   faction = tryopen(actfile, "w+");
  185.   unlink(actfile);
  186.  
  187.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  188.   fattrs = tryopen(tmpattrsfile,"w+");
  189.   unlink(tmpattrsfile);
  190.  
  191.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  192.   ftable = tryopen(tmptabfile, "w+");
  193.   unlink(tmptabfile);
  194.  
  195.     /* These are opened by `done' or `open_extra_files', if at all */
  196.   if (spec_outfile)
  197.     tabfile = spec_outfile;
  198.   else
  199.     tabfile = stringappend(name_base, base_length, ".cc");
  200.  
  201. #ifdef VMS
  202.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  203.   guardfile = stringappend(name_base, short_base_length, "_guard.cc");
  204. #else
  205.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  206.   guardfile = stringappend(name_base, short_base_length, ".guard.cc");
  207. #endif
  208. }
  209.  
  210.  
  211.  
  212. /* open the output files needed only for the semantic parser.
  213. This is done when %semantic_parser is seen in the declarations section.  */
  214.  
  215. void
  216. open_extra_files()
  217. {
  218.   FILE *ftmp;
  219.   int c;
  220.   char *filename;
  221.         /* JF change open parser file */
  222.   fclose(fparser);
  223.   filename = (char *) getenv ("BISON_HAIRY");
  224.   fparser= tryopen(filename ? filename : PFILE1, "r");
  225.  
  226.         /* JF change from inline attrs file to separate one */
  227.   ftmp = tryopen(attrsfile, "w");
  228.   rewind(fattrs);
  229.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  230.     putc(c,ftmp);
  231.   fclose(fattrs);
  232.   fattrs=ftmp;
  233.  
  234.   fguard = tryopen(guardfile, "w");
  235.  
  236. }
  237.  
  238.     /* JF to make file opening easier.  This func tries to open file
  239.        NAME with mode MODE, and prints an error message if it fails. */
  240. FILE *
  241. tryopen(name, mode)
  242. char *name;
  243. char *mode;
  244. {
  245.   FILE    *ptr;
  246.  
  247.   ptr = fopen(name, mode);
  248.   if (ptr == NULL)
  249.     {
  250.       fprintf(stderr, "bison: ");
  251.       perror(name);
  252.       done(2);
  253.     }
  254.   return ptr;
  255. }
  256.  
  257. void
  258. done(k)
  259. int k;
  260. {
  261.   if (faction)
  262.     fclose(faction);
  263.  
  264.   if (fattrs)
  265.     fclose(fattrs);
  266.  
  267.   if (fguard)
  268.     fclose(fguard);
  269.  
  270.   if (finput)
  271.     fclose(finput);
  272.  
  273.   if (fparser)
  274.     fclose(fparser);
  275.  
  276.   if (foutput)
  277.     fclose(foutput);
  278.  
  279.     /* JF write out the output file */
  280.   if (k == 0 && ftable)
  281.     {
  282.       FILE *ftmp;
  283.       register int c;
  284.  
  285.       ftmp=tryopen(tabfile, "w");
  286.       rewind(ftable);
  287.       while((c=getc(ftable)) != EOF)
  288.         putc(c,ftmp);
  289.       fclose(ftmp);
  290.       fclose(ftable);
  291.     }
  292.  
  293. #ifdef VMS
  294.   delete(actfile);
  295.   delete(tmpattrsfile);
  296.   delete(tmptabfile);
  297.   if (k==0) sys$exit(SS$_NORMAL);
  298.   sys$exit(SS$_ABORT);
  299. #else
  300.   exit(k);
  301. #endif
  302. }
  303.